﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using iHouse.Common.Const;
using iHouse.Infrastructure;
using System.Web.Routing;

namespace iHouse.Web.Infrastructure
{
    public static class HtmlHelperExtension
    {
        public static string PaginationRange(this HtmlHelper helper)
        {
            IPaginationList pagingObject = helper.ViewData.Model as IPaginationList;
            string format = "{0}-{1} of {2} {3}s";
            int currentPageIndex = pagingObject.PageIndex;

            int fromIndex = pagingObject.IsFirstPage ? 1 : (currentPageIndex - 1) * pagingObject.PageSize + 1;
            int toIndex = pagingObject.IsLastPage ? pagingObject.TotalCount : currentPageIndex * pagingObject.PageSize;

            return string.Format(format, fromIndex, toIndex, pagingObject.TotalCount, pagingObject.GenericType);
        }

        public static MvcHtmlString PaginationLink(this HtmlHelper helper, string linkText, int pageIndex)
        {
            return PaginationLink(helper, linkText, pageIndex, null);
        }

        public static MvcHtmlString PaginationLink(this HtmlHelper helper, string linkText, int pageIndex, object htmlAttributes)
        {
            var values = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;

            if (pageIndex == 0)
            {
                foreach (string key in values)
                {
                    if (string.Compare(key, PagingConst.PAGE, StringComparison.OrdinalIgnoreCase) != 0)
                        pageIndex = Convert.ToInt32(values[key]);
                }
            }

            return helper.RouteLink(linkText, string.Empty,
                new
                {
                    @page = pageIndex,
                    @action = helper.ViewContext.RequestContext.RouteData.Values["action"],
                    @controller = helper.ViewContext.RequestContext.RouteData.Values["controller"]
                }, htmlAttributes);
        }

        public static MvcHtmlString NavigationLink(this HtmlHelper helper, string linkText, string actionName,string controllerName, object routeValues, object htmlAttributes)
        {
            IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

            string iconTag = string.Empty;
            if (attributes.ContainsKey("i"))
            {
                var iconClass = attributes["i"].ToString();
                iconTag = string.Format("<i class=\"{0}\"></i>", iconClass);

                attributes.Remove("i");
            }

            if (attributes.ContainsKey("datafilter"))
            {
                var filter = attributes["datafilter"].ToString();
                attributes.Add("data-filter", filter);

                attributes.Remove("datafilter");
            }

            string actionLink = helper.ActionLink(linkText, actionName, controllerName, new RouteValueDictionary(routeValues), attributes).ToHtmlString();
            string[] links = actionLink.Split('>');
            string textWithIcon = iconTag + links[1];
            links[1] = textWithIcon;

            return MvcHtmlString.Create(string.Join(">", links));
        }
    }
}
